package me.idashu.attg.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

import me.idashu.attg.domain.AdverEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.service.AdverService;
import me.idashu.attg.util.SystemConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class AdController {

	private SystemConfig systemConfig;

	private AdverService adverService;

	public AdverService getAdverService() {
		return adverService;
	}

	@Autowired
	public void setAdverService(AdverService adverService) {
		this.adverService = adverService;
	}

	public SystemConfig getSystemConfig() {
		return systemConfig;
	}

	@Autowired
	public void setSystemConfig(SystemConfig systemConfig) {
		this.systemConfig = systemConfig;
	}

	@RequestMapping("/web/manager/ad")
	public String toAddAd(Locale locale, Model model) {
		Map<String, Object> map = new HashMap<String, Object>();
		int total = adverService.countAd();
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		
		List<AdverEntity> adverEntitys = adverService.getAllAdver(map);
		model.addAttribute("adverEntitys",adverEntitys);
		model.addAttribute("pageEntity",pageEntity);
		model.addAttribute("ad","广告管理");
		return "ad_manage";
	}

	@RequestMapping("/web/manager/ad/add")
	public String AddAd(@ModelAttribute AdverEntity adentity,
			@RequestParam(value = "file", required = false) MultipartFile file,Model model) {

		
		System.out.println("goods_name:"+adentity.getGoods_Name());
		System.out.println("postision:" + adentity.getAdver_Position());
		System.out.println("seq:" + adentity.getAdver_Seq());
		System.out.println("desc:" + adentity.getAdver_Desc());

		adentity.setAdver_Date(new Date());

		String dir = systemConfig.getDir("dir");
		String prodir = systemConfig.getDir("prodir");
		System.out.println("file------->:" + file);
		if (file.getOriginalFilename() != null
				&& !"".equals(file.getOriginalFilename())) {
			System.out.println("file:" + file.getOriginalFilename());
			try {
				InputStream is = file.getInputStream();
				FileInputStream fis = (FileInputStream) is;
				byte[] inOutb = new byte[fis.available()];
				String uuid = UUID.randomUUID().toString().replace("-", "");
				FileOutputStream fos = new FileOutputStream(new File(dir + uuid
						+ ".jpg"));
				adentity.setAdver_Image(prodir + uuid + ".jpg");
				fis.read(inOutb);
				fos.write(inOutb);
				fis.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		adverService.insertAd(adentity);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = adverService.countAd();
		PageEntity pageEntity = new PageEntity();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		map.put("pageEntity", pageEntity);
		
		List<AdverEntity> adverEntitys = adverService.getAllAdver(map);
		model.addAttribute("adverEntitys",adverEntitys);
		model.addAttribute("pageEntity",pageEntity);
		model.addAttribute("ad", "广告管理");
		return "ad_manage";
	}

	@RequestMapping("/web/manager/ad/page")
	public String adpage(@ModelAttribute PageEntity pageEntity,Locale locale, Model model) {
		Map<String, Object> map = new HashMap<String, Object>();
		int total = adverService.countAd();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());

		map.put("pageEntity", pageEntity);
		List<AdverEntity> adverEntitys = adverService.getAllAdver(map);
		model.addAttribute("adverEntitys",adverEntitys);
		model.addAttribute("pageEntity",pageEntity);
		model.addAttribute("ad", "广告管理");
		return "adajaxlist";
	}
	
	
	@RequestMapping("/web/manager/ad/delete")
	public String deletead(@RequestParam int adver_Id,@ModelAttribute PageEntity pageEntity,Locale locale, Model model) {
		
		adverService.deleteAd(adver_Id);
		
		
		System.out.println(adver_Id);
		Map<String, Object> map = new HashMap<String, Object>();
		int total = adverService.countAd();
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());

		map.put("pageEntity", pageEntity);
		List<AdverEntity> adverEntitys = adverService.getAllAdver(map);
		model.addAttribute("adverEntitys",adverEntitys);
		model.addAttribute("pageEntity",pageEntity);
		model.addAttribute("ad", "反馈列表");
		return "adajaxlist";
	}
}
